package com.jing.shop.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jing.common.core.domain.R;
import com.jing.common.core.utils.FastUtil;
import com.jing.shop.domain.ProductShop;
import com.jing.shop.domain.dto.*;
import com.jing.shop.domain.po.*;
import com.jing.shop.domain.vo.ProductShopCategoryVO;
import com.jing.shop.domain.vo.ProductShopVO;
import com.jing.shop.mapper.ProductShopMapper;
import com.jing.shop.service.ProductShopService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.concurrent.CompletableFuture;

@Service
public class ProductShopServiceImpl implements ProductShopService {

    @Autowired
    private ProductShopMapper productShopMapper;
    @Autowired
    private FastUtil fastUtil;

    @Override
    public PageInfo<ProductShopVO> findShopList(ProductShopDTO productShopDTO) {
        PageHelper.startPage(productShopDTO.getPageNum(),productShopDTO.getPageSize());
        List<ProductShopVO> shopList = productShopMapper.findShopList(productShopDTO);
        PageInfo<ProductShopVO> productShopVOPageInfo = new PageInfo<>(shopList);
        return productShopVOPageInfo;
    }

    @SneakyThrows
    @Override
    public void addShop(ProductShopDTO productShopDTO) {
        //参数校验
        checkParams(productShopDTO);
        //保存商品基本信息
        saveProductBaseInfo(productShopDTO);
        //异步处理
        CompletableFuture<Integer> cf1=CompletableFuture.supplyAsync(()->{
            //保存商品优惠信息
            savePromotionInfo(productShopDTO);
            return null;
        });
        CompletableFuture<Integer> cf2=CompletableFuture.supplyAsync(()->{
            //保存商品SKU属性信息
            saveSkuInfo(productShopDTO);
            return null;
        });
        CompletableFuture<Integer> cf3=CompletableFuture.supplyAsync(()->{
            //保存相册信息
            saveImagesInfo(productShopDTO);
            return null;
        });
        CompletableFuture<Integer> cf4=CompletableFuture.supplyAsync(()->{
            //保存商品参数
            saveParamsInfo(productShopDTO);
            return null;
        });
        cf1.get();
        cf2.get();
        cf3.get();
//        cf4.get();

    }

    private void saveParamsInfo(ProductShopDTO productShopDTO) {
        List<ProductAttributePO> productAttributePOS = productShopDTO.getProductAttributePOS();
        for (ProductAttributePO productAttributePO : productAttributePOS) {

        }
    }

    private void saveSkuInfo(ProductShopDTO productShopDTO) {
        List<ProductSkuStockPO> skuStockPOS = productShopDTO.getSkuStockPOS();
        ProductSkuStockDTO productSkuStockDTO = new ProductSkuStockDTO();
        for (ProductSkuStockPO skuStockPO : skuStockPOS) {
            productSkuStockDTO.setSkuCode(skuStockPO.getSkuCode());
            productSkuStockDTO.setLockStock(skuStockPO.getLockStock());
            productSkuStockDTO.setPrice(skuStockPO.getPrice());
            productSkuStockDTO.setPromotionPrice(skuStockPO.getPromotionPrice());
            productSkuStockDTO.setStock(skuStockPO.getStock());
            productSkuStockDTO.setSale(skuStockPO.getSale());
            productSkuStockDTO.setProductId(skuStockPO.getProductId());
        }
        productShopMapper.addSku(productSkuStockDTO);
    }

    private void saveImagesInfo(ProductShopDTO productShopDTO) {
        String[] picId = productShopDTO.getPicId();
        ProductShopPic productShopPic = new ProductShopPic();
        for (String userJSONStr : picId) {
            productShopPic.setPicImg(userJSONStr);
        }
        productShopPic.setPicId(productShopDTO.getId());
        productShopMapper.addPic(productShopPic);
    }

    private void savePromotionInfo(ProductShopDTO productShopDTO) {
        //取出优惠类型
        if(ProductShop.ONE.equals(productShopDTO.getPromotionType())){
            //无优惠
        }else if (ProductShop.TWO.equals(productShopDTO.getPromotionType())){
            //特惠促销
            ProductShopIndulgenceDTO productShopIndulgenceDTO = new ProductShopIndulgenceDTO();
            List<ProductShopIndulgencePO> shopIndulgencePOS = productShopDTO.getShopIndulgencePOS();
            for (ProductShopIndulgencePO shopIndulgencePO : shopIndulgencePOS) {
                productShopIndulgenceDTO.setIndulgenceStart(shopIndulgencePO.getIndulgenceStart());
                productShopIndulgenceDTO.setIndulgenceEnd(shopIndulgencePO.getIndulgenceEnd());
                productShopIndulgenceDTO.setIndulgencePrice(shopIndulgencePO.getIndulgencePrice());
                productShopIndulgenceDTO.setProductId(productShopDTO.getId());
                productShopMapper.addIndulgence(productShopIndulgenceDTO);
            }
        }else if (ProductShop.THREE.equals(productShopDTO.getPromotionType())){
            //会员
            ProductShopMemberDTO productShopMemberDTO = new ProductShopMemberDTO();
            List<ProductShopMemberPO> shopMemberPOS = productShopDTO.getShopMemberPOS();
            for (ProductShopMemberPO shopMemberPO : shopMemberPOS) {
                productShopMemberDTO.setMemberPrice(shopMemberPO.getMemberPrice());
                productShopMemberDTO.setMemberLevelId(shopMemberPO.getMemberLevelId());
                productShopMemberDTO.setProductId(productShopDTO.getId());
                productShopMapper.addMember(productShopMemberDTO);
            }
        } else if (ProductShop.FOUR.equals(productShopDTO.getPromotionType())) {
            //阶梯
            ProductShopStepDTO productShopStepDTO = new ProductShopStepDTO();
            List<ProductShopStepPO> shopStepPOS = productShopDTO.getShopStepPOS();
            for (ProductShopStepPO shopStepPO : shopStepPOS) {
                productShopStepDTO.setStepDiscount(shopStepPO.getStepDiscount());
                productShopStepDTO.setStepNum(shopStepPO.getStepNum());
                productShopStepDTO.setProductId(productShopDTO.getId());
                productShopMapper.addStep(productShopStepDTO);
            }
        } else if (ProductShop.FIVE.equals(productShopDTO.getPromotionType())){
            //满减
            ProductShopReductionDTO productShopReductionDTO = new ProductShopReductionDTO();
            List<ProductShopReductionPO> shopReductionPOS = productShopDTO.getShopReductionPOS();
            for (ProductShopReductionPO shopReductionPO : shopReductionPOS) {
                productShopReductionDTO.setReductionMinusprice(shopReductionPO.getReductionMinusprice());
                productShopReductionDTO.setReductionPrice(shopReductionPO.getReductionPrice());
                productShopReductionDTO.setProductId(productShopDTO.getId());
                productShopMapper.addReduction(productShopReductionDTO);
            }
        }
    }

    private void saveProductBaseInfo(ProductShopDTO productShopDTO) {
        productShopMapper.addShop(productShopDTO);
        ProductShopPO productShopPO = new ProductShopPO();
        productShopDTO.setId(productShopPO.getId());
    }

    private void checkParams(ProductShopDTO productShopDTO) {
//        if(true){
//            throw new RuntimeException("参数校验失败");
//        }
    }

    @Override
    public void updateShop(ProductShopDTO productShopDTO) {
        Integer flag = productShopMapper.updateShop(productShopDTO);
        if(flag<=0){
            throw new RuntimeException("修改异常");
        }
    }

    @Override
    public void update(ProductShopDTO productShopDTO) {
        Integer flag = productShopMapper.update(productShopDTO);
        if(flag<=0){
            throw new RuntimeException("异常");
        }
    }

    @Override
    public R upload(MultipartFile multipartFile) {
        String url="";
        try {
            url= fastUtil.upload(multipartFile);
            if(url.equals("")){
                return R.fail("上传失败");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return R.ok("http://106.54.45.36:8888"+url);
    }

    @Override
    public List<ProductAttributePO> findAttr(Integer categoryId) {
        List<ProductAttributePO> attr = productShopMapper.findAttr(categoryId);
        return attr;
    }

}
